package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.annotation.DataSource;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.entity.SysDict;
import com.entity.WorkExecuteRecord;
import com.entity.WorkOrder;
import com.entity.WorkRecord;
import com.enums.DataSourceEnum;
import com.mapper.SysDictMapper;
import com.mapper.SysUserMapper;
import com.mapper.WorkExecuteRecordMapper;
import com.mapper.WorkOrderMapper;
import com.service.SysUserService;
import com.service.WorkOrderService;
import com.service.WorkRecordService;
import com.utils.Public;

/**
* 
*
* @author Zyuan
* @version 创建时间：2019年6月22日 下午8:15:31
*/
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper,WorkOrder> implements WorkOrderService {

	@Autowired
	WorkOrderService workService;
	@Autowired
	WorkOrderMapper workOrderMapper;
	@Autowired
	WorkRecordService workRecordMapper;
	@Autowired
	SysUserService userMapper;
	@Autowired
	SysDictMapper sysDictMapper;
	@Autowired
	WorkExecuteRecordMapper executeRecordMapper;
	
	@Override
	public Page<Map<String, Object>> getWorkList(Map<String, Object> params) {
        Page<Map<String, Object>> pageBean = new Page<>(Public.mapTo(params.get("page"), 1), Public.mapTo(params.get("pageSize"), 10));
    	Map<String, Object> requestParams = new HashMap<String, Object>();
    	requestParams.put("userId", Public.mapTo(params.get("userId"), ""));
		Map<String, Object> userView  = userMapper.getUserInfo(requestParams);
        if(userView == null){
        	return new Page<>();
        }
        requestParams.clear();
        requestParams.put(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "areaLeader" : "maintainStaff", Public.mapTo(userView.get("ID"), ""));
        if(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1){
        	
            if(Public.mapTo(params.get("status"), -1) != -1 && Public.mapTo(params.get("status"), -1) != 3){
            	requestParams.put("workOrderStatus", Public.mapTo(params.get("status"), -1));
            }
            if(Public.mapTo(params.get("status"), -1) == 3){
            	requestParams.put("isRead", 0);
            }
            List<Map<String, Object>> list = workOrderMapper.getWorkList(pageBean, requestParams);
            
            pageBean.setRecords(list);
    		return pageBean;
        	
        }else{
            if(Public.mapTo(params.get("status"), -1) != -1 && Public.mapTo(params.get("status"), -1) != 3){
            	requestParams.put("workOrderStatus", Public.mapTo(params.get("status"), -1));
            }
            if(Public.mapTo(params.get("status"), -1) == 3){
            	requestParams.put("isRead", 0);
            }
            List<Map<String, Object>> list = workOrderMapper.getWorkList(pageBean, requestParams);
            
            pageBean.setRecords(list);
    		return pageBean;
        }
	}

	@Override
	public WorkOrder getWorkInfo(Map<String, Object> params) {
		String id = Public.mapTo(params.get("workOrderId"), "");
	    WorkOrder workView = workOrderMapper.selectById(id);
	    if(workView == null){
	    	return null;
	    }
	    params.clear();
	    Map<String, Object> complaint = workOrderMapper.getComplaint(workView.getComplaintId());
		workView.setComplaintView(complaint);
		
		params.put("complaintId", workView.getComplaintId());
		List<Map<String, Object>> list = workOrderMapper.getComplaintGuidanceList(params);
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> data = list.get(i);
			String [] sList = Public.mapTo(data.get("REMOTE_GUIDANCE_TYPE"), "").split(",");
			List<String> nameList = new ArrayList<>();
			List<SysDict> dictList = workService.getSbList("6294fe15eeba4dc6ad155ae00fa7d499");
			for (int k = 0; k < sList.length; k++) {
				String a =sList[k];
				for (int j = 0; j < dictList.size(); j++) {
					SysDict dict = dictList.get(j);
					if(dict.getCode().equals(Public.mapTo(a, -1))){
						nameList.add(dict.getName());
					}
				}
			}
	        data.put("nameList", nameList);
		}
		workView.setGuidanceList(list);
		
		List<WorkRecord> recordList =  workRecordMapper.selectList(new EntityWrapper<WorkRecord>()
				.eq("ORDER_ID", id));
		for (int i = 0; i < recordList.size(); i++) {
			List<SysDict> dictList = workService.getSbList("766c5893ee5c4994895c5be8053a5bf1");
			String [] sList = Public.mapTo(recordList.get(i).getSpareParts(), "").split(",");
			List<String> sparePartsName = new ArrayList<>();
			for (int j = 0; j < sList.length; j++) {
				String a = sList[j];
				for (int k = 0; k < dictList.size(); k++) {
					SysDict dict = dictList.get(k);
					if(dict.getCode().equals(Public.mapTo(a, -1))){
						sparePartsName.add(dict.getName());
					}
				}
			}
			recordList.get(i).setSparePartsName(StringUtils.join(sparePartsName, ","));
			
			for (int q = 0; q < dictList.size(); q++) {
				SysDict dict = dictList.get(q);
				if(dict.getCode().equals(Public.mapTo(recordList.get(i).getEquipmentType(), -1))){
					recordList.get(i).setEquipmentTypeName(dict.getName());
				}
			}
			
		}
		workView.setWorkRecordList(recordList);
		
		List<WorkExecuteRecord> executeRecordList = executeRecordMapper.selectList(new EntityWrapper<WorkExecuteRecord>()
				.eq("order_id", id));
		workView.setExecuteRecordList(executeRecordList);
		return workView;
	}

	@Override
	public Map<String, Object> getWorkStatusCount(Map<String, Object> params) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> userView  = userMapper.getUserInfo(params);
        if(userView == null){
        	data.put("newWorkCount", 0);
        	data.put("pendingWorkCount", 0);
        	data.put("doingWorkCount", 0);
        	data.put("doneWorkCount", 0);
        	return data;
        }

    	data.put("newWorkCount", this.selectCount(new EntityWrapper<WorkOrder>()
    			.eq("is_read", 0)
    			.eq(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "AREA_LEADER" : "MAINTAIN_STAFF", Public.mapTo(userView.get("ID"), ""))));


    	data.put("pendingWorkCount", this.selectCount(new EntityWrapper<WorkOrder>()
    			.eq("WORK_ORDER_STATUS", Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ?  0 : -1)
    			.eq(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "AREA_LEADER" : "MAINTAIN_STAFF", Public.mapTo(userView.get("ID"), "")))) ;
    
    	if(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1){
        	data.put("doingWorkCount", this.selectCount(new EntityWrapper<WorkOrder>()
        			.eq("WORK_ORDER_STATUS", "-1")
        			.eq(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "AREA_LEADER" : "MAINTAIN_STAFF", Public.mapTo(userView.get("ID"), ""))));
     
    	}else{
        	data.put("doingWorkCount", this.selectCount(new EntityWrapper<WorkOrder>()
        			.and().ne("WORK_ORDER_STATUS", 0).or().ne("WORK_ORDER_STATUS", 5)
        			.eq(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "AREA_LEADER" : "MAINTAIN_STAFF", Public.mapTo(userView.get("ID"), ""))));
    		
    	}


    	data.put("doneWorkCount", this.selectCount(new EntityWrapper<WorkOrder>()
    			.eq("WORK_ORDER_STATUS", 5)
    			.eq(Public.mapTo(userView.get("IS_AREA_LEADER"), -1) == 1 ? "AREA_LEADER" : "MAINTAIN_STAFF", Public.mapTo(userView.get("ID"), ""))));

		return data;
	}

	@Override
	@DataSource(DataSourceEnum.DB2)
	public List<SysDict> getSbList(String parentId) {
		
		return sysDictMapper.selectList(new EntityWrapper<SysDict>()
				.eq("PARENT_ID", parentId));
	}

}

